//
//  HStabBarController.swift
//  SchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

class HStabBarController: UITabBarController {
    //全局闭包属性
    var leftBtnBlock: (() -> ())?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = #colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)
        //改变按钮字体选中颜色
        tabBar.tintColor = UIColor().colorWithHex(0x00a9e9)
        //添加子控制器
       let tabDict = [["Vc":"MessageController","title":"消息","imageName":"inform"],["Vc":"HomeworkController","title":"作业","imageName":"homework"],["Vc":"ClassroomController","title":"课堂","imageName":"class_exercise"],["Vc":"ScoreController","title":"成绩","imageName":"examination"]]
        for dict in  tabDict {
            addController(dict)
        }
    }
    //重载添加方法封装
    //fileprivate私有化，支持延展
    //private完全私有，只在当前累方法中有效
    fileprivate func addController(_ dict: [String:String]) {
        // 获得命名空间并将控制器名称转换为类
        
       guard  let bundName = Bundle.main.bundleName,
        let vcName = dict["Vc"],
        let imageName = dict["imageName"],
        //使用反射机制将字符串转换为类
        let cls = NSClassFromString("\(bundName).\(vcName)") as? UIViewController.Type
        else {
            return
        }
     
        //获得当前类型
        let childController = cls.init()
        //标题
        childController.title = dict["title"]
        //nomal图片
        childController.tabBarItem.image = UIImage(named: "\(imageName)_nor")
        //设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: UIControlState.normal)
        //选中
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_press")?.withRenderingMode(.alwaysOriginal)
        //添加左边按钮
        childController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "icon_menu_nor", bgImageName: "icon_menu_press", target: self, action: #selector(leftBtnclicked))
       //导航控制器
        let navVc = HSNavigationController(rootViewController: childController)
//        添加
        addChildViewController(navVc)
    }
    ///左边按钮的监听回调方法
  @objc fileprivate  func leftBtnclicked() {
    //执行闭包，传递点击事件
    leftBtnBlock?()
    
    }
}
